//
//  TabBarViewController.m
//  DropDownListProject
//
//  Created by 花若似相离 on 2018/3/15.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "TabBarViewController.h"

#import "NavigationViewController.h"
#import "TabBar.h"

#import "ViewController.h"
#import "CenterViewController.h"
#import "PersonalViewController.h"

@interface TabBarViewController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) TabBar *tabbar;

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tabbar = [[TabBar alloc] init];
    [_tabbar.centerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //选中时的颜色
    _tabbar.tintColor = [UIColor colorWithRed:27.0/255.0 green:118.0/255.0 blue:208/255.0 alpha:1];
    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
    _tabbar.translucent = NO;
    //利用KVC 将自己的tabbar赋给系统tabBar
    [self setValue:_tabbar forKeyPath:@"tabBar"];
    
    self.delegate = self;
    [self addChildViewControllers];
}

- (void)buttonAction:(UIButton *)button
{
    self.selectedIndex = 1;//关联中间按钮
    NSLog(@"(Target事件)点击了中间按钮(在MCTabBarController中触发)");
    [self enterCenterVC];
    //    [self rotationAnimation];
}

//添加子控制器
- (void)addChildViewControllers{
    //图片大小建议32*32
    [self addChildrenViewController:[[ViewController alloc] init] andTitle:@"首页" andImageName:@"icon_tabbar_home" andSelectImage:@"icon_tabbar_home_select" andTag:1];
    //中间这个不设置东西，只占位
    [self addChildrenViewController:[[CenterViewController alloc] init] andTitle:nil andImageName:@"" andSelectImage:@"" andTag:2];
    [self addChildrenViewController:[[PersonalViewController alloc] init] andTitle:@"个人中心" andImageName:@"personal" andSelectImage:@"personal_select" andTag:3];
}

- (void)addChildrenViewController:(UIViewController *)childVC andTitle:(NSString *)title andImageName:(NSString *)imageName andSelectImage:(NSString *)selectedImage andTag:(NSInteger)tag
{
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    childVC.tabBarItem.selectedImage =  [UIImage imageNamed:selectedImage];
    childVC.title = title;
    childVC.tabBarItem.tag = tag;
    
    NavigationViewController *navigationVC = [[NavigationViewController alloc] initWithRootViewController:childVC];
    //    baseNav.tabBarItem.tag = tag;
    
    [self addChildViewController:navigationVC];
}

#pragma mark - **************** UITabBarControllerDelegate ****************
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 1){//选中中间的按钮
        NSLog(@"(代理事件)点击了中间按钮(在MCTabBarController中触发)");
        [self enterCenterVC];
        //        [self rotationAnimation];
    }else {
        //        [_mcTabbar.centerBtn.layer removeAllAnimations];
    }
}

-(void)enterCenterVC
{
    CenterViewController *centerVC = [[CenterViewController alloc] init];
    centerVC.navigationItem.title = @"发现";
    NavigationViewController *centerNC = [[NavigationViewController alloc] initWithRootViewController:centerVC];
    [self presentViewController:centerNC animated:YES completion:nil];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"tabBarController.tabBarItem.tag = %ld",viewController.tabBarItem.tag);
    NSLog(@"是否被选中");
    if (viewController.tabBarItem.tag == 0) {
        NSLog(@"不选中");
        [self enterCenterVC];
        return NO;
    }
    NSLog(@"选中");
    return YES;
}

//旋转动画
- (void)rotationAnimation{
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
    rotationAnimation.duration = 3.0;
    rotationAnimation.repeatCount = HUGE;
    [_tabbar.centerBtn.layer addAnimation:rotationAnimation forKey:@"key"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
